package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.DaylimitBean;
import org.jsbd.boss.domian.DaylimitBeanVO;
import org.jsbd.boss.service.IDaylimitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/daylimit")
public class DaylimitController {

  private static final Logger logger = LoggerFactory.getLogger(DaylimitController.class);

  @Autowired
  private IDaylimitService daylimitService;

  /**
   * 日限配置列表
   * @return
   */
  @RequestMapping(value = "/")
  public String daylimit() {
    return "daylimit/list";
  }

  /**
   * 
   * 日限配置数据
   * @param model
   * @return
   */
  @RequestMapping(value = "/listJson", method = RequestMethod.POST)
  @ResponseBody
  public Map<String, Object> listJson(DataGridModel page, DaylimitBeanVO daylimitBeanVO) {
	  String sort = daylimitBeanVO.getSort();
	  String order = daylimitBeanVO.getOrder();
	  if (StringUtils.isBlank(sort) && StringUtils.isBlank(order)) {
		  daylimitBeanVO.setSort("executeTime");
		  daylimitBeanVO.setOrder("DESC");
	  }
	  
	  String apkName = daylimitBeanVO.getApkName();
		if (StringUtils.isNotBlank(apkName)) {
			String[] apkNames = apkName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : apkNames) {
				keywords.add(keyword);
			}
			daylimitBeanVO.setKeywords(keywords);
		}
	  
	  return daylimitService.findDataWithPage(page, daylimitBeanVO);
  }
  
  /**
   * 跳转新增页面
   * @return
   */
  @RequestMapping(value = "/add", method = RequestMethod.GET)
  public String toAddPage() {
    return "daylimit/add";
  }
  
  /**
   * 新增
   * @return
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  @ResponseBody
  public void add(HttpServletResponse response, DaylimitBean daylimitBean) {
	try {
		String mark = daylimitBean.getMark();
		boolean flag = daylimitService.hasMark(mark);
		//boolean repeat = daylimitService.isRepeat(mark);
		if (!flag) {
			ResponseUtils.responseMarkNotExists(response);
		} else {
			daylimitBean.setIsExecute(0);
			daylimitService.save(daylimitBean);
			ResponseUtils.responseSuccess(response);
		}
//		if (repeat) {
//			ResponseUtils.responseMarkExists(response);
//		} 
	} catch (Exception e) {
		 ResponseUtils.responseFailure(response);
	}
  }
  
  /**
   * 跳转到编辑界面
   * 
   * @return
   */
  @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
  public String toUpdate(@PathVariable Integer id, Model model) {
    try {
      DaylimitBean daylimitBean = daylimitService.findDaylimit(id);;
      model.addAttribute("daylimitBean", daylimitBean);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
    return "daylimit/update";
  }
  
  /**
   * 修改
   */
  @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
  @ResponseBody
  public void update(@PathVariable Integer id, HttpServletResponse response, DaylimitBean daylimitBean) {
    try {
    	daylimitBean.setId(id);
    	daylimitBean.setIsExecute(0);
    	daylimitService.modifyDaylimit(daylimitBean);
    	ResponseUtils.responseSuccess(response);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      ResponseUtils.responseFailure(response);
    }
  }
  
  /**
   * 删除
   */
  @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
  public void delete (@PathVariable Integer id,  HttpServletResponse response) {
	try {
		daylimitService.delete(id);
		ResponseUtils.responseSuccess(response);
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
	    ResponseUtils.responseFailure(response);
	}
  }


}
